package jp.heartdokyun.activity;

import java.io.File;
import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {
	private SoundPool mSoundPool;
	private MediaPlayer mMediaPlayer;
	protected static boolean isEnableBGM;
	private SharedPreferences mSharedPreferences;
	private SharedPreferences.Editor mSPEditor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ImageButton ibBGM = (ImageButton) findViewById(R.id.btn_bgm);
		initSounds();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		mSharedPreferences = getPreferences(MODE_PRIVATE);
		isEnableBGM = mSharedPreferences.getBoolean("BGM_VALUE", true);
		
		mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.top_bgm);
		mMediaPlayer.setLooping(true);
		float volume = Utils.getBGMVolume(getApplicationContext());
		mMediaPlayer.setVolume(volume, volume);

		ibBGM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mSPEditor = mSharedPreferences.edit();
				isEnableBGM = !isEnableBGM;
	
				if (isEnableBGM) {
					mMediaPlayer.start();
				} else {
					mMediaPlayer.pause();
					mMediaPlayer.seekTo(0);
				}

				mSPEditor.putBoolean("BGM_VALUE", isEnableBGM);
				mSPEditor.commit();
			}
		});
		ImageButton ibStart = (ImageButton) findViewById(R.id.ib_main_start_game);
		ibStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				playSound(1, 0);
				Intent itMenu = new Intent(MainActivity.this,
						MenuActivity.class);
				startActivity(itMenu);
			}
		});
		if (mSharedPreferences.getBoolean("NARUSE_FIRST_TIME", true)) {
			File file = new File(Environment.getExternalStorageDirectory()
					.toString() + "/NaruseKokomi");
			Utils.deleteRecursive(file);
			mSPEditor = mSharedPreferences.edit();
			mSPEditor.putBoolean("NARUSE_FIRST_TIME", false);
			mSPEditor.commit();
			String[] arrType = getResources().getStringArray(R.array.type_name);
			for(int i=0;i<arrType.length;i++){
				Utils.addLevel(this, arrType[i]);
			}
			
		}

		ImageButton ibCardCollection = (ImageButton) findViewById(R.id.ib_main_card_collection);
		ibCardCollection.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				playSound(2, 0);
				Intent itMenu = new Intent(MainActivity.this,
						CardCollectionActivity.class);
				itMenu.putExtra("BGM_VALUE", isEnableBGM);
				startActivity(itMenu);
			}
		});
		ImageButton ibHelp = (ImageButton) findViewById(R.id.btn_help);
		ibHelp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
//				playSound(2, 0);
				Intent itHelp = new Intent(MainActivity.this,
						HelpActivity.class);
//				itHelp.putExtra("BGM_VALUE", isEnableBGM);
				startActivity(itHelp);
			}
		});
	}

	@Override
	public void onStart() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				playSound(3, 0);
			}
		}).run();
		super.onStart();
	}

	@Override
	public void onResume() {
		// if (isEnableBGM) {
		// mMediaPlayer.start();
		// } else {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		// }
		mMediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.top_bgm);
		float volume = Utils.getBGMVolume(getApplicationContext());
		
		Log.d("myvolume",String.valueOf(volume));
		mMediaPlayer.setVolume(volume, volume);
		mMediaPlayer.setLooping(true);
		if (isEnableBGM) {
			mMediaPlayer.start();
		}

		super.onResume();
	}

	@Override
	public void onPause() {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		mMediaPlayer.release();
		super.onPause();
	}

	@Override
	public void onDestroy() {
		mSoundPool.release();
		mMediaPlayer.release();
		super.onDestroy();
	}

	public static final int SOUND_EXPLOSION = 1;

	private HashMap<Integer, Integer> soundPoolMap;

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1,
				mSoundPool.load(getApplicationContext(), R.raw.start, 1));
		soundPoolMap.put(2, mSoundPool.load(getApplicationContext(),
				R.raw.sokohadameeen_sexy, 1));
		soundPoolMap.put(3, mSoundPool.load(getApplicationContext(),
				R.raw.narusekokominoheartnidokyun, 1));
	}

	public void playSound(int sound, int loop) {
		float volume = Utils.getVoiceVolume(getApplicationContext());
		mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
	}

}
